$(document).ready(function(){

    $('#giftlist-wrapper .giftlist-box:last').css({ 'margin-right' : '0' });
	createWishList("#giftlist-wrapper .giftlist-pane");
	
	
	//SCROLLPANE
	//$('#giftlist-wrapper .giftlist-pane').width( $('.#giftlist-wrapper .giftlist-box').size() * 240 );
	
	if( $("#giftlist-wrapper .giftlist-box").size()<=4 ){
		$("#giftlist-wrapper .scroll-bar").hide();
		$("#selectui").hide();
	}
	
	//scrollpane parts
	var scrollPane = $( "#giftlist-wrapper" );
	var scrollContent = $('#giftlist-wrapper .giftlist-pane');
    $('#giftlist-wrapper .giftlist-box:last').css({ 'margin-right' : '0' });
    $(scrollContent).width( ( $('.giftlist-box').size() * 240 ) - 6 );
    
	
	//build slider
	var scrollbar = $( ".scroll-bar" ).slider({
		slide: function( event, ui ) {
			if ( scrollContent.width() > scrollPane.width() ) {
				scrollContent.css( "margin-left", Math.round(
					ui.value / 100 * ( scrollPane.width() - scrollContent.width() )
				) + "px" );
			}else {
				scrollContent.css( "margin-left", 0 );
			}
		}
	});
	
	//append icon to handle
	var handleHelper = scrollbar.find( ".ui-slider-handle" )
	.mousedown(function() {
		scrollbar.width( handleHelper.width() );
	})
	.mouseup(function() {
		scrollbar.width( "100%" );
	})
	.append( "<span class='ui-icon ui-icon-grip-dotted-vertical'></span>" )
	.wrap( "<div class='ui-handle-helper-parent'></div>" ).parent();
	
	//change overflow to hidden now that slider handles the scrolling
	scrollPane.css( "overflow", "hidden" );
	
	
	
	//size scrollbar and handle proportionally to scroll distance
	function sizeScrollbar() {
		var remainder = scrollContent.width() - scrollPane.width();
		var proportion = remainder / scrollContent.width();
		var handleSize = scrollPane.width() - ( proportion * scrollPane.width() );


		scrollbar.find( ".ui-slider-handle" ).css({
			width: handleSize
		});
		handleHelper.width( "" ).width( scrollbar.width() - handleSize );
	}

	//reset slider value based on scroll content position
	function resetValue() {
		var remainder = scrollPane.width() - scrollContent.width();
		var leftVal = scrollContent.css( "margin-left" ) === "auto" ? 0 :
			parseInt( scrollContent.css( "margin-left" ) );
		var percentage = Math.round( leftVal / remainder * 100 );
		scrollbar.slider( "value", percentage );
	}

	//if the slider is 100% and window gets larger, reveal content
	function reflowContent() {
			var showing = scrollContent.width() + parseInt( scrollContent.css( "margin-left" ), 10 );
			var gap = scrollPane.width() - showing;
			if ( gap > 0 ) {
				scrollContent.css( "margin-left", parseInt( scrollContent.css( "margin-left" ), 10 ) + gap );
			}
	}
	
	//change handle position on window resize
	$( window ).resize(function() {
		resetValue();
		sizeScrollbar();
		reflowContent();
	});
	//init scrollbar size
	setTimeout( sizeScrollbar, 10 );//safari wants a timeout
    
    Cufon.replace('#giftlist-wrapper .giftlist-box .giftlist-link', {
        fontFamily: 'Proxima Nova Rg'
    });
    
});


//FUNCTIONS
function createWishList(target){
	var nome;
	var uri;
	var status;
	var qtd;
	var preco;
	var remover;
	var msg;
	var img;
	var container;
	var counter = 0;
	var paneWidth;
	$(".giftlistsku .giftlistsku-table tbody tr").each(function(){
		img 	= $(this).find("td.giftlistsku-body-image img").attr("src");
		nome 	= $(this).find("td.giftlistsku-body-product a").text();
		uri 	= $(this).find("td.giftlistsku-body-product a").attr("href");
		status 	= $(this).find("td.giftlistsku-body-status").text();
		qtd 	= $(this).find("td.giftlistsku-body-qtde").text();
		preco 	= $(this).find("td.giftlistsku-body-value").text();
		remover = $(this).find("td.giftlistsku-body-remove input").val();
		msg 	= $(this).find("td.giftlistsku-body-message").text();
		
		container = 
				"<div class='giftlist-box'>" +
					"<a href='javascript:void(0)' class='giftlist-link'>" +
						"<img src='"+img+"' width='229' height='229' alt='' />" +
						"<span class='shadow'></span>" +
						"<span class='produto-nome'>"+nome+"</span>" +
						"<span class='produto-preco'>"+preco+"</span>" +
						"<span class='produto-remover'><img src='/Control/ArquivoExibir.aspx?NomeArquivo=img_remover-wishlist.png' class='remove-"+remover+" 'width='33' height='33' alt='remover' /></span>" +
					"</a>" +
				"</div>";
		
		$(target).append( $(container) );
		counter ++;
	});
	
	//calcula o tamanho do pane com os produtos
	paneWidth = counter * (229+11); //229 = tamanho da div ||| 11 tamanho da margem
	$("#giftlist-wrapper .giftlist-pane").css("width",paneWidth+"px");
	
	//evento que remove o item da lista
	$("#giftlist-wrapper .giftlist-box span.produto-remover img").click(function(){
		cbVal = $(this).attr("class").substring(7);
		removeWishList(cbVal);
		
		$(this).parent().parent().fadeOut("slow");
		$('#giftlist-wrapper .giftlist-pane').width( ($('#giftlist-wrapper .giftlist-pane').width() - 240) +"px" );
	});	
    
    $('#giftlist-wrapper .giftlist-box:last').css({ 'margin-right' : '0' });
    
}

function removeWishList(cbValue){
	$(".giftlistsku .giftlistsku-table tbody tr td.giftlistsku-body-remove input").each(function(){
		if( $(this).val() == parseInt(cbValue) ){
			$(this).attr('checked', true);
			$("#giftlistremovesku").trigger("click");
			return false;
		}
	});
}